package com.fanqwz.kotlinmvvm.ui.compositor

import android.content.Context
import android.databinding.DataBindingComponent
import android.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.fanqwz.kotlinmvvm.R
import com.fanqwz.kotlinmvvm.databinding.ActivityProgressBinding
import com.fanqwz.kotlinmvvm.presenter.viewmodel.ProgressViewModel

/**
 * Created by fanqwz on 2017/11/6.
 */
class ProgressCompositor(val context: Context,
                         private val dataBindingComponent: DataBindingComponent,
                         private val enableBehavior: io.reactivex.Observable<Boolean>) : ViewCompositor() {
    private lateinit var binding: ActivityProgressBinding
    private val viewModel = ProgressViewModel()

    override fun addView(root: ViewGroup, child: View?, compositor: ViewCompositor) {
        binding.content.addView(child)
    }

    override fun inflateView(layoutInflater: LayoutInflater): View? {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_progress, null, false, dataBindingComponent)!!
        binding.progress = viewModel
        enableBehavior.subscribe {
            viewModel.visibility.set(if (it)  View.VISIBLE else View.GONE)
//            binding.content?.visibility = if (it) View.INVISIBLE else View.VISIBLE
//            binding.content?.isEnabled = !it
            binding.interceptorRoot.interceptEvent = it
        }
        return binding.root

    }


}